#include "widget.h"
#include "ui_widget.h"

#include <QShortcut>
#include <QSlider>

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
    // 1.通过Slider调整窗口大小
    changeWindowSize();

    // 2.通过按键调整Slider的值
    setSliderByShortcut();
}

Widget::~Widget()
{
    delete ui;
}

void Widget::setSliderByShortcut()
{
    ui->horizontalSlider_2->setMinimum(0);
    ui->horizontalSlider_2->setMaximum(100);
    ui->horizontalSlider_2->setSingleStep(10);
    ui->horizontalSlider_2->setValue(0);

    QString str = QString::number(ui->horizontalSlider_2->value());
    ui->label->setText(str);

    QShortcut* shortCut1 = new QShortcut(this);
    shortCut1->setKey(QKeySequence("-"));
    connect(shortCut1, &QShortcut::activated, this, &Widget::subValue);

    QShortcut* shortCut2 = new  QShortcut(this);
    shortCut2->setKey(QKeySequence("="));
    connect(shortCut2, &QShortcut::activated, this, &Widget::addValue);
}

void Widget::subValue()
{
    int value = ui->horizontalSlider_2->value();
    ui->horizontalSlider_2->setValue(value-10);
}

void Widget::addValue()
{
    int value = ui->horizontalSlider_2->value();
    ui->horizontalSlider_2->setValue(value+10);

}

void Widget::on_horizontalSlider_2_valueChanged(int value)
{
    ui->label->setText(QString::number(value));
}








void Widget::changeWindowSize()
{
    ui->horizontalSlider->setMinimum(0);
    ui->horizontalSlider->setMaximum(1000);
    ui->horizontalSlider->setValue(500);
    ui->horizontalSlider->setSingleStep(100);

    ui->verticalSlider->setMinimum(0);
    ui->verticalSlider->setMaximum(1000);
    ui->verticalSlider->setValue(500);
    ui->verticalSlider->setSingleStep(100);
    // 反转调整方向
    ui->verticalSlider->setInvertedAppearance(true);
}

void Widget::on_horizontalSlider_valueChanged(int value)
{
    QRect rect = this->geometry();
    this->setGeometry(rect.x(), rect.y(), value, rect.height());
}

void Widget::on_verticalSlider_valueChanged(int value)
{
    QRect rect = this->geometry();
    this->setGeometry(rect.x(), rect.y(), rect.width(), value);
}

